//菜单Layer，包括投降，谈和，强制谈和，设置

var battleMenu = {
	showing : false,
	
	_menu : null,
	_tipLabel : null,
	
	_AddMenu : function(text, xpos, ypos, callbackfunction) {
		var label = cc.LabelTTF.create(text, "simhei", 48);
		var menuItem = new cc.MenuItemLabel(label, function callback(sender){
			cc.audioEngine.playEffect(resource.click_wav);
			sender.setColor(cc.color(255,64,0));
			//no bad result of layer-destroy
			setTimeout(function(){
				sender.setColor(cc.color(255,255,255));
				callbackfunction(sender);
			}, 0.25 * 1000);
		}, this);

		menuItem.attr({x:xpos, y:ypos});
		return menuItem;
	},
	
	Initialize : function(){
		this.showing = false;
		this._menu = null;
		this._tipLabel = null;
	},
	
	ShowUI : function(){
		if (this.showing)
			return;
		
		var winSize = cc.director.getWinSize();
		
		var xcenter = winSize.width * 0.5;
		var ycenter = winSize.height * 0.5;
		var space = 0.12 * winSize.height;
		
		this._menu = cc.Menu.create(
			this._AddMenu("结束战役（投降）", xcenter, ycenter + 2 * space, this._OnSurrendClick),
			this._AddMenu("停战议和", xcenter,		ycenter + 0.8 * space, this._OnSummationClick),
			this._AddMenu("强制停战议和", xcenter,	ycenter - 0.4 * space, this._OnForceSumClick),
			this._AddMenu("游戏设置", xcenter,		ycenter - 2 * space, this._OnSettingClick)
		);
		this._menu.attr({x:0, y:0});
		campaignScene.canvaslayer.addChild(this._menu);
		
		this._tipLabel = cc.LabelTTF.create("（条件: 超过30回合 或  战力比大于 4:1）", "simhei", 26);
		this._tipLabel.attr({anchorX:0.5, x:xcenter, y:ycenter - 1.0 * space, color:cc.color(220,220,220)});
		campaignScene.canvaslayer.addChild(this._tipLabel);
		
		this.showing = true;
	},
	
	HideUI : function(){
		if (this.showing == false)
			return;
		
		if (this._menu != null){
			this._menu.removeFromParent();
			this._menu = null;
		}
		if (this._tipLabel != null){
			this._tipLabel.removeFromParent();
			this._tipLabel = null;
		}
		
		this.showing = false;
	},

	OnSurrend : null,
	_OnSurrendClick : function(sender){
		battleMenu.HideUI();
		control.menuBackMenu.enabled = false;
		if (battleMenu.OnSurrend != null)
			battleMenu.OnSurrend();
		battleSum.ShowUI(false);
	},
	
	OnSummation : null,
	_OnSummationClick : function(sender){
		battleMenu.HideUI();		
		control.menuBackMenu.enabled = false;
		if (battleMenu.OnSummation != null)
			battleMenu.OnSummation();
		battleSum.ShowUI(true);
	},
	
	OnForceSum : null,
	_OnForceSumClick : function(sender){
		battleMenu.HideUI();		
		if (battleMenu.OnForceSum != null)
			battleMenu.OnForceSum();
		battleSum.ShowUI(true);
	},
	
	_OnSettingClick : function(sender){
		battleMenu.HideUI();
		campaignScene.canvaslayer.hide(false);
		SetAllManipulateEnable(true);
		
		campaignScene.scene.addChild(new SettingLayer(true, true), 10);
	}
};

control.onMenuBackClick = function(sender){
	if (battleMenu.showing){
		battleMenu.HideUI();

		SetAllManipulateEnable(true);
		campaignScene.canvaslayer.hide(false);
	}else{
		battleMenu.ShowUI();

		SetAllManipulateEnable(false);
		control.menuBackMenu.enabled = true;

		campaignScene.canvaslayer.show(true);
	}
};